package com.jjb.common.view;


import com.jjb.common.view.draw.GrawCanvas;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class ImageUtil {

    private static final String TRANSPARENT_IMAGE_FROMAT = "npg";

    /**
     * 图片压缩
     * @param srcImagePath：原图路径
     * @param tarImgPath：  目标图路径
     * @param windth： 宽度(设置0时表示按高度等比例缩放)
     * @param height： 高度(设置0时表示按宽度等比例缩放)
     * @param quality: 质量
     */
    public static void narrowImageFile(String srcImagePath,
                                       String tarImgPath,
                                       int windth,
                                       int height,
                                       float quality) {
        if(srcImagePath == null || srcImagePath.trim().length() == 0) {
            throw new RuntimeException("原图路径不能为空");
        }

        if(tarImgPath == null || tarImgPath.trim().length() == 0) {
            throw new RuntimeException("目标路径不能为空");
        }

        if(windth <= 0 && height <= 0) {
            throw new RuntimeException("高度宽度不能同时小于等于0");
        }

        File srcImgFile = new File(srcImagePath);
        if(!(srcImgFile.exists() && srcImgFile.isFile())) {
            throw new RuntimeException("原图路径不存在");
        }


        try {


            BufferedImage bufferedImage = ImageIO.read(srcImgFile);
            int srcWidth = bufferedImage.getWidth();  //原图的宽度
            int srcHeight = bufferedImage.getHeight(); //原图的高度


            if( windth <= 0 && height <= 0 ) {
                //当高度宽度都未设置则不压缩图片大小，只压缩图片质量
                windth = srcWidth;
                height = srcHeight;
            } else {
                if(windth <= 0 ) {  //需要等比例压缩宽度
                    double bili = NumberUtil.div(srcHeight, height, RoundingMode.DOWN, 10);
                    windth = BigDecimal.valueOf(NumberUtil.div(srcWidth, bili, RoundingMode.DOWN, 0)).intValue();
                }

                if(height <= 0) {  //需要等比例压缩高度
                    double bili = NumberUtil.div(srcWidth, windth, RoundingMode.DOWN, 10);
                    double d = NumberUtil.div(srcHeight, bili, RoundingMode.DOWN, 0);
                    height = BigDecimal.valueOf(d).intValue();
                }

            }
            Thumbnails.of(srcImagePath).size(windth, height).outputQuality(quality).toFile(tarImgPath);
            /*
            String subfix = tarImgPath.substring(tarImgPath.lastIndexOf(".")+1, tarImgPath.length());

            BufferedImage targetBufferedImage = null;
            if(subfix.equalsIgnoreCase(TRANSPARENT_IMAGE_FROMAT)) {
                targetBufferedImage = new BufferedImage(windth, height, BufferedImage.TYPE_INT_ARGB);
            } else {
                targetBufferedImage = new BufferedImage(windth, height, BufferedImage.TYPE_INT_RGB);
            }



            Graphics2D graphics = targetBufferedImage.createGraphics();
            graphics.setBackground(Color.WHITE);
            graphics.setColor(Color.WHITE);

            graphics.drawImage(bufferedImage.getScaledInstance(windth, height, Image.SCALE_SMOOTH), 0, 0, null);
            graphics.dispose();

            ImageIO.write(targetBufferedImage, subfix, new File(tarImgPath));

*/
        }catch (Exception e) {
            throw new RuntimeException("图片压缩失败:" + e.getMessage(), e);
        }

    }
    

    /**
     * 图片处理
     * @param srcImgPath：原图
     * @param tarImgPath：输出端目标图
     * @param grawCanvas： 画布处理类
     */
    public static void imageDrawCancas(String srcImgPath, String tarImgPath, List<GrawCanvas> grawCanvas) throws IOException{
        FileOutputStream outImgStream = null;
        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);


            if(grawCanvas != null) {
                for(GrawCanvas contentWrite : grawCanvas) {
                    contentWrite.draw(g);
                }
            }



            g.dispose();

            // 输出图片
            outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "png", outImgStream);
            outImgStream.flush();


        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if(outImgStream != null) {
                try { outImgStream.close(); } catch (IOException e) { }
            }
        }
    }
}
